#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>

#include <QDebug>
#include <QSlider>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/res/music.png"));
    setWindowTitle("音乐播放器");
    medplayer = new QMediaPlayer(this); // 确保它是有效的实例
//    setStyleSheet("QWidget { "
//                  "background-image: url(:/res/nailong.jpg); "
//                  "background-repeat: no-repeat; "
//                  "background-position: center; "
//                  "background-size: contain; "
//                  "}");

    //首先写界面，很简单..
    //获取音乐，播放音乐
    //控件与数据的交互
    //自动播放下一首 没写

    //获取当前媒体的时长
    connect(medplayer,&QMediaPlayer::durationChanged,this,[=](qint64 duration){
        ui->totallabel->setText(QString("%1:%2").arg(duration/1000/60,2,10,QChar('0')).arg(duration/1000%60));
        ui->horizontalSlider->setRange(0,duration);
        totalTime=duration;
    });
    //获取当前播放时长
    connect(medplayer,&QMediaPlayer::positionChanged,this,[=](qint64 pos){
        ui->curlabel->setText(QString("%1:%2").arg(pos/1000/60,2,10,QChar('0')).arg(pos/1000%60,2,10,QChar('0')));
        ui->horizontalSlider->setValue(pos);

    });
    //拖动进度条
    connect(ui->horizontalSlider,&QSlider::sliderMoved,this,[=](int position){
        medplayer->setPosition(position);
    });


    //设置音量
    connect(ui->verticalSlider,&QSlider::sliderMoved,this,[=](int position){
        medplayer->setVolume(position);
            ui->verticalSlider->setValue(position);
    });


    ui->verticalSlider->setVisible(false);
    medplayer->setVolume(20);
    ui->verticalSlider->setRange(0,100);
    ui->verticalSlider->setValue(20);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::musicPlay()
{

    qDebug() <<playlist[curindex];
    medplayer->setMedia(playlist[curindex]);

    // 播放
    medplayer->play();
    if (medplayer->state() == QMediaPlayer::PlayingState) {
        qDebug() << "Music is playing";
    } else {
        qDebug() << "Failed to play music";
    }
}


void Widget::on_pushButton_clicked()
{
    // 打开文件对话框，让用户选择音乐所在的目录
       QString path = QFileDialog::getExistingDirectory(this, "请选择音乐所在的目录", "E:/KwDownload");

       if (path.isEmpty()) {
           QMessageBox::warning(this, "警告", "未选择任何目录");
           return;
       }
       // 根据这个路径，获取里面的所有的mp3、wav文件
       QDir dir(path);
       QStringList musicList = dir.entryList(QStringList() << "*.mp3" << "*.wav");

       // 将这些文件添加到列表控件中
       ui->listWidget->addItems(musicList);
       //默认选中第一个音乐
       ui->listWidget->setCurrentRow(0);

       for(auto file:musicList){
           playlist.append(QUrl::fromLocalFile(path+"/"+file));
       }




}

void Widget::on_pushButton_4_clicked()
{

    if(playlist.empty()){
        return;
    }
    switch (medplayer->state()) {
        case QMediaPlayer::StoppedState: // 停止状态
            //播放当前选中的音乐
            curindex=ui->listWidget->currentRow();
            musicPlay();

            // 设置按钮文本为 "stop"
            ui->pushButton_4->setText("stop");
            break;
        case QMediaPlayer::PlayingState: // 播放状态
            // 如果现在正在播放，暂停音乐
            medplayer->pause();
            ui->pushButton_4->setText("start");
            break;
        case QMediaPlayer::PausedState: // 暂停状态
            // 如果现在是暂停的，继续播放音乐
            medplayer->play();
            ui->pushButton_4->setText("stop");
            break;
        default:
            break;
        }







}




void Widget::on_pushButton_last_clicked()
{
    //上一曲

    //播放当前选中的音乐
    if(curindex==0){
        curindex=playlist.size()-1;
    }else{
        curindex=(curindex-1)%playlist.size();
    }
    qDebug() <<curindex;
    musicPlay();

    ui->listWidget->setCurrentRow(curindex);
    // 设置按钮文本为 "stop"
    ui->pushButton_4->setText("stop");
}

void Widget::on_pushButton_next_clicked()
{

    curindex=(curindex+1)%playlist.size();
    qDebug() <<curindex;
    musicPlay();

    ui->listWidget->setCurrentRow(curindex);
    // 设置按钮文本为 "stop"
    ui->pushButton_4->setText("stop");
}


void Widget::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    curindex=ui->listWidget->row(item);
    musicPlay();
    ui->listWidget->setCurrentRow(curindex);
    // 设置按钮文本为 "stop"
    ui->pushButton_4->setText("stop");
}

void Widget::on_pushButton_sound_clicked()
{
    if(ui->verticalSlider->isVisible()){
        ui->verticalSlider->setVisible(false);
    }
    else
    {
        ui->verticalSlider->setVisible(true);
    }

}

void Widget::on_pushButton_list_clicked()
{
    if(ui->listWidget->isVisible()){
        ui->listWidget->setVisible(false);
    }
    else
    {
        ui->listWidget->setVisible(true);
    }
}

void Widget::on_listWidget_itemClicked(QListWidgetItem *item)
{
    if(QMediaPlayer::StoppedState||QMediaPlayer::PausedState){
        curindex=ui->listWidget->currentRow();
        musicPlay();

        // 设置按钮文本为 "stop"
        ui->pushButton_4->setText("stop");
    }
}
